package com.ego.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ego.common.vo.member.MemberReceiveAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.member.po.MemberReceiveAddress;
import com.ego.member.service.MemberReceiveAddressService;
import com.ego.common.util.Result;

/**
 * @author KamTang
 * @program: ego
 * @description 会员收货地址控制器
 * @date 2022-11-13 13:28:12
 */
@RestController
@RequestMapping("member/receive-address")
public class MemberReceiveAddressController {

    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    /**
     * 根据会员ID获取地址信息
     */
    @GetMapping("/gets/{memberId}")
    public Result gets(@PathVariable("memberId") Long memberId) {
        List<MemberReceiveAddress> memberReceiveAddresses = memberReceiveAddressService.gets(memberId);
        List<MemberReceiveAddressVo> memberReceiveAddressVos = memberReceiveAddresses.stream()
                .map(MemberReceiveAddress::toVo).collect(Collectors.toList());
        return Result.ok().put("memberReceiveAddressVos", memberReceiveAddressVos);
    }

    /**
     * 根据地址id获取收货信息
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id) {
        MemberReceiveAddress memberReceiveAddress = memberReceiveAddressService.getById(id);
        MemberReceiveAddressVo memberReceiveAddressVo = memberReceiveAddress.toVo();
        return Result.ok().put("memberReceiveAddressVo", memberReceiveAddressVo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody MemberReceiveAddress memberReceiveAddress) {
        memberReceiveAddressService.save(memberReceiveAddress);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody MemberReceiveAddress memberReceiveAddress) {
        memberReceiveAddressService.updateById(memberReceiveAddress);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        memberReceiveAddressService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
